package controllers;

import models.AdminUser;
import models.User;

import org.apache.commons.lang.StringUtils;

import play.cache.Cache;

public class Security extends Secure.Security{

	static boolean authentify(String username, String password) {
		boolean uconnected = User.connect(username, password);
		boolean aconnected = AdminUser.connect(username, password);
		 if(aconnected || uconnected){
			 session.put("username", username);
			 //TODO 登录日志
		 }
		 return aconnected || uconnected;
	}

	static boolean check(String profile) {
		//TODO 判断权限
		if ("admin".equals(profile)) {
			if(!StringUtils.isEmpty(Security.connected())){
				//TODO 缓存
				return AdminUser.find("byUsername", Security.connected()).first() != null;
			}
		}
		return false;
	}
	
	static void onDisconnect(){
		session.remove("username");
	}

	static void onDisconnected() {
		Application.index();
	}

	static void onAuthenticated() {
		if(check("admin")){
			redirect("/admin");
		}
		Application.index();

	}

}
